﻿using System;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using System.Data;
using System.Collections.Generic;
using VIT.Library.Web;

namespace VIT.Web.Admin
{
    public partial class PageEdit : VITComponent
	{
        VIT.Provider.Page obj; 

        protected void Page_Load(object sender, EventArgs e)
        {
            Language language = new Language();
            obj = new VIT.Provider.Page();
            obj.Id = Request["sId"] == null ? "" : Request["sId"];

            if (obj.Id == "") ButtonInsert.Visible = true; else ButtonModify.Visible = true;

            if (!IsPostBack)
            {
                loadLanguages(language, obj.LanguageId);
                loadData();                
            }
        }

        #region Methods
        private void loadData()
        {
            obj.LanguageId = Session["language"].ToString();
            obj.GetSingle();

            if (obj.Id != "")
            {
                txtId.Text = obj.Id;
                ArticleTitle.Text = obj.Title;
                Description.Text = obj.Description;
                Contents.Value = obj.Contents;
                Tag.Text = obj.Tag;
                Orders.Text = obj.Orders.ToString();
                if (!string.IsNullOrEmpty(obj.Image))
                {
                    img.Visible = true;
                    img.ImageUrl = URLRoot + "Uploads/Images/Pages/" + obj.Image;
                    img.AlternateText = obj.Image;
                }
                txtId.ReadOnly = true;
            }            
        }

        private void loadLanguages(Language language, string selectedId)
        {
            LanguageId.DataSource = language.getAll();
            LanguageId.DataTextField = "Name";
            LanguageId.DataValueField = "Id";
            LanguageId.DataBind();

            if (string.IsNullOrEmpty(selectedId)) LanguageId.SelectedValue = Session["language"].ToString();
            else LanguageId.SelectedValue = selectedId;
        }

        private void save()
        {
            // bat loi tren server
            var data = new Dictionary<dynamic, List<object>> {
                                                        {txtId, new List<object> {"required"}},
                                                        {ArticleTitle, new List<object> {"required"}},
                                                        {Orders, new List<object> {"number"}}
                                                    };
            if (MainCore.checkData(data))
            {
                bool imgvalid = true;
                if (fupImg.HasFile)
                {
                    if (MainCore.checkFile(fupImg.FileName, "isImage"))
                    {
                        string oldImg = img.AlternateText;
                        obj.Image = String.Format("{0:yyMMddhhmmss}", DateTime.Now) + System.IO.Path.GetExtension(fupImg.FileName);
                        try
                        {
                            fupImg.SaveAs(URLBaseDir + "Uploads/Images/Pages/" + obj.Image);
                            if(System.IO.File.Exists(URLBaseDir + "Uploads/Images/Pages/" + oldImg))
                                System.IO.File.Delete(URLBaseDir + "Uploads/Images/Pages/" + oldImg);
                        }
                        catch (System.NotSupportedException ex)
                        {
                            throw ex;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    else
                    {
                        imgvalid = false;
                        setTrueVisible(lblInvalidImage);
                    }
                }
                else obj.Image = img.AlternateText;

                if (imgvalid)
                {
                    obj.Id = txtId.Text;
                    obj.LanguageId = Session["language"].ToString();
                    obj.Title = ArticleTitle.Text;
                    obj.Description = Description.Text;
                    obj.Contents = Contents.Value;
                    obj.Tag = Tag.Text;
                    if (Orders.Text != "")
                        obj.Orders = int.Parse(Orders.Text);

                    if (obj.Save()) setTrueVisible(lblThanhCong);
                    else setTrueVisible(lblThatBai);
                }
            }
            else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;
            lblInvalidImage.Visible = false;

            control.Visible = true;
        }
        #endregion

        #region Events
        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Page", "PageList");
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
            loadData();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Page", "PageList");
        }

        protected void LanguageId_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["language"] = LanguageId.SelectedValue;
            loadData();
        }
        #endregion
    }
}